<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      p {
        height: 300px;
        border: solid;
        font-size: 40px;
      }
      #top {
        width: 100px;
        height: 100px;
        background-color: bisque;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 40px;
        position: fixed;
        bottom: 60px;
        right: 20px;
        cursor: pointer;
      }

      img {
        height: 200px;
      }
    </style>
  </head>
  <body>
    <div id="top">TOP</div>
    <p>
      hello p1
      <img data-src="./imgs/b1.jpg" src="./imgs/loading.gif" alt="" />
    </p>
    <p>hello p2</p>
    <p>hello p3</p>
    <p>hello p4</p>
    <p>hello p5</p>
    <p>
      hello p6
      <img data-src="./imgs/b2.jpg" src="./imgs/loading.gif" alt="" />
    </p>
    <p>hello p7</p>
    <p>hello p8</p>
    <p>
      hello p9

      <img data-src="./imgs/b3.jpg" src="./imgs/loading.gif" alt="" />
    </p>
    <script>
      function showImg() {
        const imgs = document.querySelectorAll(" img");
        imgs.forEach((el) => {
          const { y } = el.getBoundingClientRect();
          if (el.dataset.src) {
            if (y < window.innerHeight - 200) {
              el.src = el.dataset.src;
              el.dataset.src = "";
            }
          }
        });
      }

      showImg();

      window.onscroll = function () {
        showImg();
        const r =
          document.documentElement.scrollHeight -
            window.pageYOffset -
            window.innerHeight <
          60;
        if (r) {
          // 新内容
          const p = document.createElement("p");
          p.innerHTML = "new P";
          document.body.append(p);
        }
      };
    </script>
  </body>
</html>
